let URL = '';
let URLJV = '';

import util from '../common/util.js';
// import md5 from '../common/md5.js';
// import CryptoJS from '../common/crypto-js-4.0.0'
import CryptoJS from 'crypto-js';
import md5 from 'js-md5';
// let httpType = uni.getStorageSync('httpType');
let $locality = 0;//1 本地部署  默认0  2本地大改动  ($locality需要修改http.js、main.js(两个)、util.js)(为2时 pages.json里面5S需改为6S，首页要放在第一)
// $locality = httpType;
//#ifndef MP-WEIXIN

if (process.env.NODE_ENV === 'development') {
	console.log('开发环境');
	// URL = 'http://www.ygkj.com:8081/index.php/';
	if($locality==0){
		URL = 'http://api.leanplants.com/';
		// URLJV = 'http://192.168.1.23:8081/';
		URLJV = 'https://jvapi.leanplants.com/';
	}else{
		// URL = 'http://10.8.8.221:18080/';
		// URLJV = 'http://10.8.8.221:18081/';
		URL = 'https://comac-szfj.e.lanxin.cn/samcSixS-php/';
		URLJV = 'https://comac-szfj.e.lanxin.cn/samcSixS-java/';
		// URL = 'http://jygl.leanplanttec.com:18080/';
	}
	
} else {
	console.log('生产环境');
	 // URL = 'http://www.ygkj.com:8081/index.php/';
	 if($locality==0){
	 	URL = 'http://api.leanplants.com/';
	 	URLJV = 'https://jvapi.leanplants.com/';
	 }else{
	 	// URL = 'http://10.8.8.221:18080/';
	 	// URLJV = 'http://10.8.8.221:18081/';
		URL = 'https://comac-szfj.e.lanxin.cn/samcSixS-php/';
		URLJV = 'https://comac-szfj.e.lanxin.cn/samcSixS-java/';
		// URL = 'http://jygl.leanplanttec.com:18080/';
	 }
}
// if (process.env.NODE_ENV === 'development') {
// 	console.log('开发环境');
// 	// URL = 'http://10.72.1.72:8080/';
// 	URL = 'http://14.22.81.54:18080/';
// } else {
// 	console.log('生产环境');
// 	// URL = 'http://10.72.1.72:8080/';
// 	URL = 'http://14.22.81.54:18080/';
// }
// #endif
// // #ifdef MP-WEIXIN
// URL = 'http://api.leanplants.com/';
// // #endif
// URL = 'http://apiv2.leanplants.com/';
switch (uni.getSystemInfoSync().platform) {
	case 'android':
		console.log('运行Android上')
		break;
	case 'ios':
		console.log('运行iOS上')
		break;
	default:
		console.log('运行在开发者工具上')
		break;
};

const http = (url, data, method,loading=true) => {
	// let httpType = uni.getStorageSync('httpType');
	// $locality = httpType;
	// if($locality==0){
	// 	URL = 'http://api.leanplants.com/';
	// 	URLJV = 'https://jvapi.leanplants.com/';
	// }else{
	// 	URL = 'http://10.72.1.72:18080/';
	// 	URLJV = 'http://10.72.1.72:8081/';
	// }
	url = 'apicloud/' + url;
	let formartDate = util.formartDate();
	let api_token = md5(`${url}${formartDate}leanplants1234567896s987654159`);
	data = {
		...data,
		api_token: api_token,
		// business_type:1,//1博雄APP(打包博雄时需要)
		client_id: 1
	}
	let token = uni.getStorageSync('token');
	// let token = '15998903933';
	// let token = 'c8de093853b93d120d06a2273ec44f170eb04a83';
	// let token = '18883292847';
	// if (token && token != '') {
	// 	data = {
	// 		...data,
	// 		token: token
	// 	}
	// };
	if (token && token != '') {
		data = {
			...data,
		}
	};
	if(url == 'apicloud/runningState/dataMiningStatus'||url == 'apicloud/running_state/dataMiningStatus'){//设备数采实时状态
		loading = false
	}
	if(loading){
		uni.showLoading({
			title:'加载中',
			mask:true
		});
	}
	// console.log(data);
	return new Promise(function(resolve, reject) {
		uni.request({
			url: URL + url || '',
			method: method || 'POST',
			data: data || null,
			header: {
				"Content-Type": "application/x-www-form-urlencoded", // application/json
				// "Access-Token":uni.getStorageSync('jwt') || '',
			},
			success: function(res) {
				uni.hideLoading();
				resolve(res.data);
			},
			fail: function(err) {
				uni.hideLoading();
				console.log(data);
				console.log(JSON.stringify(err));
				reject({
					error: '网络错误'
				});
			},
		})
	})
}
const httpJava = (url, data, method) => {
	let aes_key = '6jqvnrd3t32u288w';
	let aes_iv = 'abc123rty456nji7';
	let key = 'aJeE7zYWFAoJESSS6l8hRkSK5As9Fh2v';
	console.log(data)
	let timestamp = parseInt(new Date().getTime() / 1000);
	let encrypt = CryptoJS.AES.encrypt(JSON.stringify(data), CryptoJS.enc.Utf8.parse(aes_key), {
	    iv: CryptoJS.enc.Utf8.parse(aes_iv), mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7
	});
	let en_data = encrypt.toString();
	let sign = md5(key + timestamp);
	
	let token = uni.getStorageSync('token');
	// let token = '15998903933';
	// let token = 'dfdd29ef24638232b57dae484d85f0693869dea4';
	// let token = 'd30c5b85d3a5c612a380447039c620e13a510bd9';
	// let token = 'f4a728e75a6089d9e7598c37571866f2cdcebae1';
	let datas = {
		"en_data": en_data,
		"timestamp": timestamp,
		"sign": sign
	}
	if (token && token != '') {
		datas = {
			...datas,
			token: token
		}
	};
	// console.log(data);
	return new Promise(function(resolve, reject) {
		uni.request({
			url: URLJV + url || '',
			method: method || 'POST',
			data: datas || null,
			header: {
				// "Content-Type": "application/x-www-form-urlencoded", // application/json
				// "Access-Token":uni.getStorageSync('jwt') || '',
				// 'Access-Control-Allow-Origin':'*',
				// "Authorization": token?token:''
			},
			success: function(res) {
				uni.hideLoading();
				resolve(res.data);
			},
			fail: function(err) {
				uni.hideLoading();
				console.log(data);
				console.log(JSON.stringify(err));
				reject({
					error: '网络错误'
				});
			},
		})
	})
}
export {http,httpJava};
